{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ec36e04a-2fd1-46f3-93f6-6b1a7c43d16a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "ca_meta = pd.read_csv('../ca/ca_meta.csv')\n",
    "gla_meta = ca_meta[(ca_meta.District == 7) | (ca_meta.District == 8) | (ca_meta.District == 12)]\n",
    "gla_meta = gla_meta.reset_index()\n",
    "gla_meta = gla_meta.drop(columns=['index'])\n",
    "gla_meta.to_csv('gla_meta.csv', index=False)\n",
    "print(gla_meta[gla_meta.duplicated(subset=['Lat', 'Lng'])])\n",
    "gla_meta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7176890b-1900-42d2-8ee1-423a411c3897",
   "metadata": {},
   "outputs": [],
   "source": [
    "gla_meta_id2 = gla_meta.ID2.values.tolist()\n",
    "print(len(gla_meta_id2))\n",
    "\n",
    "ca_rn_adj = np.load('../ca/ca_rn_adj.npy')\n",
    "print(ca_rn_adj.shape)\n",
    "\n",
    "gla_rn_adj = ca_rn_adj[gla_meta_id2]\n",
    "gla_rn_adj = gla_rn_adj[:,gla_meta_id2]\n",
    "print(gla_rn_adj.shape)\n",
    "\n",
    "np.save('gla_rn_adj.npy', gla_rn_adj)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4df7b8be-6f11-44ec-99ec-dd71298bc158",
   "metadata": {},
   "outputs": [],
   "source": [
    "year = ''  # please specify the year, our experiments use 2019\n",
    "\n",
    "gla_meta.ID = gla_meta.ID.astype(str)\n",
    "gla_meta_id = gla_meta.ID.values.tolist()\n",
    "\n",
    "ca_his = pd.read_hdf('../ca/ca_his_' + year +'.h5')\n",
    "gla_his = ca_his[gla_meta_id]\n",
    "gla_his"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3733a22b-4252-4a48-ad9b-e20c062278ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "gla_his.to_hdf('gla_his_' + year + '.h5', key='t', mode='w')\n",
    "gla_his.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22f8a21f-3127-413a-a97a-a3b4c937632b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e41fa33a-8ac0-4d88-9c20-21e855803b7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# code for checking adj stat\n",
    "gla_rn_adj = np.load('gla_rn_adj.npy')\n",
    "node_num = gla_rn_adj.shape[0]\n",
    "\n",
    "print(gla_rn_adj[0,0])\n",
    "gla_rn_adj[np.arange(node_num), np.arange(node_num)] = 0\n",
    "print(gla_rn_adj[0,0])\n",
    "\n",
    "print('edge number', np.count_nonzero(gla_rn_adj))\n",
    "print('node degree', np.mean(np.count_nonzero(gla_rn_adj, axis=-1)))\n",
    "print('sparsity', np.count_nonzero(gla_rn_adj) / (node_num**2) * 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7e82eeb-26d0-4528-8889-40335b814dbb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch",
   "language": "python",
   "name": "torch"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
